<template>
  <view @click="choAdr()">
    <!-- 添加地址的按钮 -->
    <view class="btn-box" v-if="JSON.stringify(address) === '{}'">
      <button @click="choAdr" type="primary" size="mini" class="adress-btn">请选择收获地址+</button>
    </view>
    <!-- 地址信息 -->
    <view class="adress-msg" v-else>
      <view class="msg-top">
        <view class="mt-left">
          收获人：{{address.userName}}
        </view>
        <view class="mt-right">
          电话：{{address.telNumber}} >
        </view>

      </view>
      <view class="msg-bottom">
        <view class="mb-left">
          收获地址：
        </view>
        <view class="mb-right">
          {{addr}}
        </view>
      </view>
    </view>
    <!-- 分割线 -->
    <image src="/static/fgx.png" class="fgx-sty"></image>
  </view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
  export default {
    name: "my-adress",
    data() {
      return {
        // adress:{}
      };
    },
    computed: {
      ...mapState ('my_user',['address']),
      ...mapGetters ('my_user',['addr'])
    },
    methods: {
      ...mapMutations('my_user',['updataAddress']),
      async choAdr() {
          const [err, succ] = await uni.chooseAddress().catch(err => err)
      
          // 2. 用户成功的选择了收货地址
          if (err === null && succ.errMsg === 'chooseAddress:ok') {
            // 为 vuex 里面的收货地址对象赋值
            this.updataAddress(succ)
             // console.log(this.address)
          }
          // 用户没有授权
          if (err && (err.errMsg === 'chooseAddress: fail auth deny'|| err.errMsg === 'chooseAddress:fail authorize no response')) {
            this.reAuth()
          }
    },
      async reAuth() {
        const [err2,confirmResult] = await uni.showModal({
          content: '检测到您没有打开地址权限，是否区设置打开',
          confirmText: '确认',
          cancelText: '取消'
        })
        if (err2) return
        console.log(confirmResult)
        
        if(confirmResult.cancel) return uni.showMsg('您取消了地址授权')
        if (confirmResult.confirm) return uni.openSetting({
          success(settingResult) {
            if (settingResult.authSetting['scope.address']) {
              return uni.showMsg('授权成功！请选择收获地址')
            }else {
              return uni.showMsg('你取消了授权')
            }
          }
        })
      }
  },
  }
</script>

<style lang="scss">
  .btn-box {
    width: 100%;
    height: 100px;
    background-color: #aaffff;
    display: flex;
    justify-content: center;
    align-items: center;

    .adress-btn {}
  }

  .adress-msg {
    display: flex;
    flex-direction: column;
    margin: 5px 10px;

    .msg-top {
      margin-bottom: 10px;
      display: flex;
      justify-content: space-between;

      .mt-left {}

      .mt-right {}
    }

    .msg-bottom {
      display: flex;
      justify-content: space-between;

      .mb-left {
        display: flex;
        align-items: center;
        white-space: nowrap;
      }

      .mb-right {}
    }
  }

  .fgx-sty {
    width: 100%;
    height: 5px;

  }
</style>
